package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizDevice;
import cn.stylefeng.guns.modular.gridsystem.entity.BizDeviceStatusAssess;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizDeviceMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizDeviceParam;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizDeviceStatusAssessParam;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizExceptionParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizDeviceExceptionRecordResult;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizDeviceResult;
import cn.stylefeng.guns.modular.gridsystem.service.BizDeviceService;
import cn.stylefeng.guns.modular.gridsystem.service.BizDeviceStatusAssessService;
import cn.stylefeng.guns.modular.gridsystem.service.BizExceptionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备表 服务实现类
 *
 * @author ssy
 * @date 2021/09/14 11:47
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BizDeviceServiceImpl extends ServiceImpl<BizDeviceMapper, BizDevice> implements BizDeviceService {

    private final BizExceptionService bizExceptionService;

    private final BizDeviceStatusAssessService bizDeviceStatusAssessService;

    @Override
    public PageResult<BizDeviceResult> page(BizDeviceParam param) {

        // 构造条件
        LambdaQueryWrapper<BizDevice> queryWrapper = new LambdaQueryWrapper<BizDevice>()
                .eq(ObjectUtil.isNotEmpty(param.getStationId()), BizDevice::getStationId, param.getStationId())
                .eq(ObjectUtil.isEmpty(param.getStationId()), BizDevice::getStationId, 0)
                .eq(ObjectUtil.isNotEmpty(param.getIndustryId()), BizDevice::getIndustryId, param.getIndustryId())
                .orderByDesc(BizDevice::getCreatedAt);

        // 查询分页结果
        return new PageResult<>(this.baseMapper.pageList(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BizDeviceResult> list(BizDeviceParam param) {

        // 构造条件
        LambdaQueryWrapper<BizDevice> queryWrapper = new LambdaQueryWrapper<BizDevice>()
                .eq(ObjectUtil.isNotEmpty(param.getStationId()), BizDevice::getStationId, param.getStationId())
                .eq(ObjectUtil.isNotEmpty(param.getId()),BizDevice::getId, param.getId());

        return this.list(queryWrapper)
                .stream()
                .map(item->BeanUtil.toBean(item,BizDeviceResult.class))
                .collect(Collectors.toList());
    }

    @Override
    public void add(BizDeviceParam bizDeviceParam) {

        // 将dto转为实体
        BizDevice bizDevice = new BizDevice();
        BeanUtil.copyProperties(bizDeviceParam, bizDevice);

        this.save(bizDevice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizDeviceParam bizDeviceParam) {
        //删除设备对应故障日志
        final BizExceptionParam bizExceptionParam = new BizExceptionParam();
        bizExceptionParam.setDeviceId(bizDeviceParam.getId());
        bizExceptionService.delete(bizExceptionParam);


        //删除设备对应的设备状态评估日志
        final BizDeviceStatusAssessParam deviceStatusAssessParam = new BizDeviceStatusAssessParam();
        deviceStatusAssessParam.setDeviceId(bizDeviceParam.getId());
        final List<BizDeviceStatusAssess> assessList = bizDeviceStatusAssessService.list(deviceStatusAssessParam);
        assessList.forEach(bizDeviceStatusAssess -> {
            final BizDeviceStatusAssessParam statusAssessParam = new BizDeviceStatusAssessParam();
            statusAssessParam.setId(bizDeviceStatusAssess.getId());
            statusAssessParam.setDeviceId(bizDeviceStatusAssess.getDeviceId());
            bizDeviceStatusAssessService.delete(statusAssessParam);
        });

        this.removeById(bizDeviceParam.getId());
    }

    @Override
    public void edit(BizDeviceParam bizDeviceParam) {

        // 根据id查询实体
        BizDevice bizDevice = this.queryBizDevice(bizDeviceParam);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizDeviceParam, bizDevice);
        this.updateById(bizDevice);
    }

    @Override
    public BizDevice detail(BizDeviceParam bizDeviceParam) {
        return this.queryBizDevice(bizDeviceParam);
    }

    @Override
    public BizDeviceResult queryById(Integer id) {
        return baseMapper.queryById(id);
    }

    @Override
    public List<BizDeviceExceptionRecordResult> getDeviceException(Integer stationId,String startTime,String endTime) {
        return this.baseMapper.getDeviceExecption(stationId,startTime,endTime);
    }


    /**
     * 获取设备表
     *
     * @author ssy
     * @since 2021/09/14 11:47
     */
    private BizDevice queryBizDevice(BizDeviceParam bizDeviceParam) {
        BizDevice bizDevice = this.getById(bizDeviceParam.getId());
        return bizDevice;
    }
}
